package com.program.facesigninsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.entity.SignInLink;
import com.program.facesigninsystem.iteration.dto.SignInActivityByTemplate;
import com.program.facesigninsystem.iteration.dto.TeacherInfo;
import com.program.facesigninsystem.iteration.dto.SigninactivityInfo;
import com.program.facesigninsystem.iteration.entity.*;
import com.program.facesigninsystem.iteration.mapper.*;
import com.program.facesigninsystem.service.IRewardService;
import com.program.facesigninsystem.service.ISignInActivityService;
import com.program.facesigninsystem.service.NTeacherService;
import com.program.facesigninsystem.service.SignInTemplateService;
import com.program.facesigninsystem.util.ExcelUtil;
import com.program.facesigninsystem.util.GetInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class NTeacherServiceImpl implements NTeacherService {

    @Resource
    UserMapper userMapper;

    @Resource
    NoteMapper noteMapper;


    @Override
    public List<TeacherInfo> selectList(String name,String university, String academy) {
        QueryWrapper<UserEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("issued",1);
        userQueryWrapper.like((name!=null && name != ""),"name",name);
        userQueryWrapper.like((university!=null && university != ""),"university",university);
        userQueryWrapper.like((academy!=null && academy != ""),"academy",academy);
        List<UserEntity> userInfos = userMapper.selectList(userQueryWrapper);
        List<TeacherInfo> teacherInfoList = new ArrayList<>();
        for(UserEntity user : userInfos ){
            TeacherInfo teacherInfo = new TeacherInfo().setUid(user.getUid())
                    .setName(user.getName())
                    .setSex(user.getSex())
                    .setAge(user.getAge())
                    .setAddress(user.getAddress())
                    .setPhone(user.getPhone())
                    .setEmail(user.getEmail())
                    .setUniversity(user.getUniversity())
                    .setAcademy(user.getAcademy())
                    .setAvatar(user.getAvatar());
            teacherInfoList.add(teacherInfo);
        }
        return teacherInfoList;
    }

    @Override
    public TeacherInfo selectListByid(String uid) {
        QueryWrapper<UserEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("issued",1);
        userQueryWrapper.eq("uid",uid);
        UserEntity user = userMapper.selectOne(userQueryWrapper);
        TeacherInfo teacherInfo = new TeacherInfo().setUid(user.getUid())
                .setName(user.getName())
                .setSex(user.getSex())
                .setAge(user.getAge())
                .setAddress(user.getAddress())
                .setPhone(user.getPhone())
                .setEmail(user.getEmail())
                .setUniversity(user.getUniversity())
                .setAcademy(user.getAcademy())
                .setAvatar(user.getAvatar());
        return teacherInfo;
    }


    @Override
    public void addnote(String uid,String note) {
        QueryWrapper<NoteEntity> userQueryWrapper = new QueryWrapper<>();

        NoteEntity noteEntity = new NoteEntity();
        noteEntity.setUid(uid);
        noteEntity.setNote(note);
        noteMapper.insert(noteEntity);
    }

    @Override
    public List<NoteEntity> getnotebyid(String uid) {
        QueryWrapper<NoteEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uid",uid);
        List<NoteEntity> noteEntityList = noteMapper.selectList(userQueryWrapper);
        return noteEntityList;
    }


    @Override
    public void exportExcel( HttpServletResponse response) {

        List<TeacherInfo> teacherInfoList = selectList("","","");

        //标题
        String[] titile = {"姓名","性别","年龄","住址","联系方式","电子邮箱","所属大学","所属学院"};
        //excel文件名
        String fileName = "教师信息导出表"+System.currentTimeMillis()+".xls";
        //sheet名
        String sheetName = "教师信息表";

        String[][] content;
        content = new String[teacherInfoList.size()][];
        for(int i = 0 ; i < teacherInfoList.size(); i++){
            content[i] = new String[titile.length];
            TeacherInfo teacherInfo = teacherInfoList.get(i);
            content[i][0] = teacherInfo.getName();
            content[i][1] = teacherInfo.getSex();
            content[i][2] = teacherInfo.getAge()!=null ? teacherInfo.getAge().toString() : "";
            content[i][3] = teacherInfo.getAddress()!=null ? teacherInfo.getAddress(): "";
            content[i][4] = teacherInfo.getPhone()!=null ? teacherInfo.getPhone() : "";
            content[i][5] = teacherInfo.getEmail()!=null ? teacherInfo.getEmail() : "";
            content[i][6] = teacherInfo.getUniversity()!=null ? teacherInfo.getUniversity() : "";
            content[i][7] = teacherInfo.getAcademy()!=null ? teacherInfo.getAcademy() : "";
        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,titile,content,null);

        //响应到客户端
        try{
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }


    }
    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}